<?php

class UsersController extends AppController {
	
	public $uses = array('User','Department');
	
	public function beforeFilter() {
		parent::beforeFilter();
    	
	}
	
	public function login() {
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$this->redirect($this->Auth->redirect());
			} else {
				$this->Session->setFlash(__('Invalid username or password, try again'));
			}
		}
	}
	
	public function logout() {
		$this->redirect($this->Auth->logout());
	}
	
	public $paginate = array(
        'limit' => 20,
        'order' => array(
            'User.username' => 'asc'),
		'conditions' => array('User.id !=' => '2')
    );

    public function index() {
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'admin') { 
			$this->User->recursive = -1;
			$this->set('users', 
						$this->paginate()); 
		}
		else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }
		
        
    }

    public function view($id = null) {
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'admin') { 
			$this->User->id = $id;
			if (!$this->User->exists()) {
				throw new NotFoundException(__('Invalid user'));
			}
			$this->set('user', $this->User->read(null, $id));
			$this->set('departments', $this->Department->find('all'));//gat the department from the departments table
		
		}else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }
    }

    public function add() {
		if ($this->Session->read('Auth.User.role') === 'superadmin') {
			if ($this->request->is('post')) {
				$this->User->create();
				if ($this->User->save($this->request->data)) {
					$this->Session->setFlash(__('The user has been saved'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
				}
			}
			$this->set('departments', $this->Department->find('all'));//get the department from the departments table
		}
		else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }
		
        
    }

    public function edit($id = null) {
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'admin') {
			
			$this->User->id = $id;
			if (!$this->User->exists()) {
				throw new NotFoundException(__('Invalid user'));
			}
			if ($this->request->is('post') || $this->request->is('put')) {
				$this->request->data['User']['leavedays'] = json_encode($this->data['leavedays']); //Added this line
				if ($this->User->save($this->request->data)) {
					$this->Session->setFlash(__('The user has been saved'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
				}
			} else {
				$this->request->data = $this->User->read(null, $id);
				unset($this->request->data['User']['password']);
			}
			
			$user = $this->User->findById($id);
			$this->set('user', $user);
			$this->set('departments', $this->Department->find('all'));//gat the department from the departments table
		}
		else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }
		
        
    }

    public function delete($id = null) {
		if ($this->Session->read('Auth.User.role') === 'superadmin') {
			if (!$this->request->is('post')) {
				throw new MethodNotAllowedException();
			}
			$this->User->id = $id;
			if (!$this->User->exists()) {
				throw new NotFoundException(__('Invalid user'));
			}
			if ($this->User->delete()) {
				$this->Session->setFlash(__('User deleted'));
				$this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('User was not deleted'));
			$this->redirect(array('action' => 'index'));
		}
		else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }
		
        
    }
	
	/*public function changepassword($id = null){
		$this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['User']['password_created'] = date('Y-m-d'); //Added this line
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The password has been saved'));
                $this->redirect(array('controller'=>'timesheets','action' => 'index'));
            } else {
                $this->Session->setFlash(__('The password could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
	}*/
	
	public function changepassword($id = null) {
		$this->User->id = $id;  // Set User Id
		if (!$this->User->exists()) {
			throw new NotFoundException(__('Invalid user'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['User']['password_created'] = date('Y-m-d'); //Added this line
			if ($this->User->save($this->request->data)) {
				$this->Session->setFlash('Password has been changed.');
			} else {
				$this->Session->setFlash('Password could not be changed.');
			}
		} else {
				//$this->data = $this->User->findById($this->Auth->user('id'));
		}
    }
}

?>
